<template>
    <view class="container">
        <uni-section title="简历信息" type="line" class="section">
            <uni-forms ref="resumeForm" :modelValue="resume">
                <uni-forms-item label="个人邮箱" required>
                    <uni-easyinput v-model="resume.personalEmail" placeholder="请输入个人邮箱" />
                </uni-forms-item>
                <uni-forms-item label="个人证书">
                    <uni-easyinput v-model="resume.personalCertificates" placeholder="请输入个人证书" />
                </uni-forms-item>
                <uni-forms-item label="个人优势">
                    <uni-easyinput v-model="resume.personalStrengths" placeholder="请输入个人优势" />
                </uni-forms-item>
                <uni-forms-item label="求职意向">
                    <uni-easyinput v-model="resume.jobSearch" placeholder="请输入求职意向" />
                </uni-forms-item>
                <uni-forms-item label="工作经历">
                    <uni-easyinput v-model="resume.workExperience" placeholder="请输入工作经历" />
                </uni-forms-item>
                <uni-forms-item label="教育经历">
                    <uni-easyinput v-model="resume.educationalExperience" placeholder="请输入教育经历" />
                </uni-forms-item>
                <u-button type="primary" @click="updateResume">更新简历</u-button>
            </uni-forms>
        </uni-section>
    </view>
</template>

<script setup>
import { ref } from 'vue';
import { http } from '@/utils/request.js';

// 初始化 resume 数据对象
const resume = ref({
    id: null,
    personalEmail: '',
    personalCertificates: '',
    personalStrengths: '',
    jobSearch: '',
    workExperience: '',
    educationalExperience: ''
});

// 缓存初始简历数据
let initialResume = { ...resume.value };

// 页面加载时获取之前存储的简历数据 (uni-app 使用 onLoad)
onLoad(() => {
    const savedResume = uni.getStorageSync('jobSeekerData');
    
    if (savedResume && savedResume.educationalExperience !== undefined) {
        resume.value = savedResume;
        initialResume = { ...savedResume };  // 保存原始数据用于后续对比
    } else {
        console.log("没有找到简历数据或缺少某些字段");
    }
});

// 离开页面时提示保存 (uni-app 使用 onUnload)
onUnload(() => {
    const hasChanges = Object.keys(resume.value).some(key => resume.value[key] !== initialResume[key]);
    
    if (hasChanges) {
        // 如果表单有改动，弹出提示框
        uni.showModal({
            title: '提示',
            content: '您有未保存的修改，是否保存？',
            success: (res) => {
                if (res.confirm) {
                    updateResume();  // 如果用户选择保存，调用保存函数
                } else {
                    console.log('用户选择不保存');
                }
            }
        });
    }
});

const updateResume = async () => {
    try {
        const response = await http({
            url: '/jobSeeker/updateResume',
            method: 'POST',
            data: resume.value,
        });

        if (response.flag) {
            uni.showToast({
                title: '更新成功',
                icon: 'success'
            });
        } else {
            uni.showToast({
                title: '更新失败：' + response.shuju,
                icon: 'none'
            });
        }
    } catch (error) {
        console.error("请求错误：", error);
        uni.showToast({
            title: '系统错误',
            icon: 'none'
        });
    }
};
</script>

<style scoped>
.container {
    padding: 20px;
    background-color: #f9f9f9;
}
.section {
    margin-bottom: 20px;
}
</style>
